package org.linlinjava.litemall.db.service;

import com.github.pagehelper.PageHelper;
import org.linlinjava.litemall.db.dao.LitemallUserMapper;
import org.linlinjava.litemall.db.domain.LitemallUser;
import org.linlinjava.litemall.db.domain.LitemallUserExample;
import org.linlinjava.litemall.db.domain.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

@Service
public class LitemallUserService {
    @Autowired
    private LitemallUserMapper userMapper;

    public LitemallUser findById(Integer userId) {
        return userMapper.selectByPrimaryKey(userId);
    }

    public UserVo findUserVoById(Integer userId) {
        LitemallUser user = findById(userId);
        UserVo userVo = new UserVo();
        userVo.setNickname(user.getNickname());
        userVo.setAvatar(user.getAvatar());
        return userVo;
    }

    public LitemallUser queryByOid(String openId) {
        LitemallUserExample example = new LitemallUserExample();
        example.or().andWeixinOpenidEqualTo(openId).andDeletedEqualTo(false);
        return userMapper.selectOneByExample(example);
    }

    public void add(LitemallUser user) {
        user.setAddTime(LocalDateTime.now());
        user.setUpdateTime(LocalDateTime.now());
        userMapper.insertSelective(user);
    }

    public int updateById(LitemallUser lmuser) {
        LitemallUser user = new LitemallUser();
        user.setId(lmuser.getId());
        if(lmuser.getDiscount()>10){
            user.setDiscount(lmuser.getDiscount()-10);
        }
        user.setUserLevel((byte)(lmuser.getUserLevel()+1));
        user.setUpdateTime(LocalDateTime.now());
        return userMapper.updateByPrimaryKeySelective(user);
    }
    public int updateFatherById(LitemallUser lmuser) {
        LitemallUser user = new LitemallUser();
        user.setId(lmuser.getId());
        user.setRegistnum(lmuser.getRegistnum()+1);
        user.setUpdateTime(LocalDateTime.now());
        return userMapper.updateByPrimaryKeySelective(user);
    }
    public List<LitemallUser> querySelective(String username, String mobile, Integer page, Integer size, String sort, String order) {
        LitemallUserExample example = new LitemallUserExample();
        LitemallUserExample.Criteria criteria = example.createCriteria();

        if (!StringUtils.isEmpty(username)) {
            criteria.andUsernameLike("%" + username + "%");
        }
        if (!StringUtils.isEmpty(mobile)) {
            criteria.andMobileEqualTo(mobile);
        }
        criteria.andDeletedEqualTo(false);

        if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
            example.setOrderByClause(sort + " " + order);
        }

        PageHelper.startPage(page, size);
        return userMapper.selectByExample(example);
    }

    public int count() {
        LitemallUserExample example = new LitemallUserExample();
        example.or().andDeletedEqualTo(false);

        return (int) userMapper.countByExample(example);
    }

    public List<LitemallUser> queryByUsername(String username) {
        LitemallUserExample example = new LitemallUserExample();
        example.or().andUsernameEqualTo(username).andDeletedEqualTo(false);
        return userMapper.selectByExample(example);
    }

    public boolean checkByUsername(String username) {
        LitemallUserExample example = new LitemallUserExample();
        example.or().andUsernameEqualTo(username).andDeletedEqualTo(false);
        return userMapper.countByExample(example) != 0;
    }

    public List<LitemallUser> queryByMobile(String mobile) {
        LitemallUserExample example = new LitemallUserExample();
        example.or().andMobileEqualTo(mobile).andDeletedEqualTo(false);
        return userMapper.selectByExample(example);
    }

    public List<LitemallUser> queryByOpenid(String openid) {
        LitemallUserExample example = new LitemallUserExample();
        example.or().andWeixinOpenidEqualTo(openid).andDeletedEqualTo(false);
        return userMapper.selectByExample(example);
    }

    public void deleteById(Integer id) {
        userMapper.logicalDeleteByPrimaryKey(id);
    }
    
    public LitemallUser queryByRecomendcode(String recomendcode) {
        return userMapper.queryByrecomendcode(recomendcode);
    }

    public LitemallUser findByUserName(String username) {
        return userMapper.selectByUserName(username);
    }

    public void updateByName(Integer parentid,String username,String recommendCode) {
        LitemallUser user = new LitemallUser();
        user.setParentid(parentid);
        user.setRecommendcode(recommendCode);
        user.setDiscount(98);
        user.setUpdateTime(LocalDateTime.now());
        user.setUsername(username);
        userMapper.updateByName(user);
    }

    public List<LitemallUser> findByParentId(Integer parentId) {
       return userMapper.findByParentId(parentId);
    }

    public void updateParentById(LitemallUser user2,Integer parentId) {
        LitemallUser user = new LitemallUser();
        user.setParentid(parentId);
        user.setUpdateTime(LocalDateTime.now());
        user.setUsername(user2.getUsername());

        userMapper.updateByName(user);
    }

    public int updateMoneyById(LitemallUser lmuser) {
        LitemallUser user = new LitemallUser();
        user.setId(lmuser.getId());
        user.setMoney(lmuser.getMoney());
        user.setUpdateTime(LocalDateTime.now());
        return userMapper.updateByPrimaryKeySelective(user);
    }
}
